package com.pda.smallbug.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.pda.smallbug.myapplication.httpDomain.LoginHttp;
import com.pda.smallbug.myapplication.httpDomain.ResetHttp;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/6/27.
 */

public class ResetActivity extends Activity implements View.OnClickListener{
    private Spinner spinner;
    private PopupWindow popupWindow;
    private LinearLayout name_layout;
    private RecyclerView recyclerView;
    private ImageView name_icon;
    private List<LoginHttp.LoginData> data;
    private ResetAdapter adapter;
    private int selectPosition=0;
    private TextView name;
    private EditText editText,xin_mima,xin_mima_again;
    private Button reset;
    private TextView system;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset);

        EventBus.getDefault().register(this);
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        name_layout=(LinearLayout)findViewById(R.id.name_layout);
        name=(TextView)findViewById(R.id.name);
        name_icon=(ImageView) findViewById(R.id.name_icon);
        editText=(EditText)findViewById(R.id.editText);
        xin_mima=(EditText)findViewById(R.id.xin_mima);
        xin_mima_again=(EditText)findViewById(R.id.xin_mima_again);
        reset=(Button)findViewById(R.id.reset);
        system=(TextView)findViewById(R.id.system);

        setPopupWindow();
        addListener();
        initData();

    }
    private void addListener(){
        name_layout.setOnClickListener(this);
        reset.setOnClickListener(this);
        system.setOnClickListener(this);
    }

    private void initData(){
        String username=MyApplication.mySharedPreferences.getString("username","");
        if (!username.isEmpty()){
            name.setText(username);
        }
        if (MyApplication.mySharedPreferences.getString("url","").isEmpty()){
            Toast.makeText(ResetActivity.this,"请设置服务器ip",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(ResetActivity.this,SystemAcitvity.class);
            startActivity(intent);
            reset.setEnabled(false);
        }else {
            getData();
        }
    }

    private void getData(){
        reset.setEnabled(false);
        RequestParams requestParams=new RequestParams(MyApplication.url+"tools/submit_ajax.ashx");
        requestParams.setConnectTimeout(1000);
        requestParams.addBodyParameter("action","getuserlist");
        x.http().get(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.e("asd",result);
                Gson gson=new Gson();
                LoginHttp http= gson.fromJson(result,LoginHttp.class);
                if (http.getStatus()==1){
                    data=http.getData();
                    adapter.update(data);
                    reset.setEnabled(true);
                }else {
                    Toast.makeText(ResetActivity.this,http.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(ResetActivity.this,"网络请求失败，请检查服务器ip",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }
    private void setPopupWindow(){
        View contentView = LayoutInflater.from(ResetActivity.this).inflate(R.layout.poplayout, null);
        recyclerView=(RecyclerView)contentView.findViewById(R.id.recyclerView);
        adapter=new ResetAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(ResetActivity.this,LinearLayoutManager.VERTICAL,false));
        popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT,  300, true);
        popupWindow.setTouchable(true);
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.transpant));
        // 设置好参数之后再show

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                name_icon.setImageResource(R.mipmap.login_xia);
            }
        });

    }

    public void onEventMainThread(MyEvent event)
    {
        if (event.getCode()==0){
            getData();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        switch (id){
            case R.id.name_layout:
                popupWindow.showAsDropDown(name_layout,0,0);
                name_icon.setImageResource(R.mipmap.login_shang);
                break;
            case R.id.reset:
                if (name.getText().toString().equals("请选择用户名")){
                    Toast.makeText(ResetActivity.this,"请选择用户名",Toast.LENGTH_SHORT).show();
                }else if (editText.getText().toString().isEmpty()){
                    Toast.makeText(ResetActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }else if (!editText.getText().toString().equals(data.get(selectPosition).getFpsd())){
                    Toast.makeText(ResetActivity.this,"旧密码错误",Toast.LENGTH_SHORT).show();
                }else if (xin_mima.getText().toString().isEmpty()){
                    Toast.makeText(ResetActivity.this,"请输入新密码",Toast.LENGTH_SHORT).show();
                }else if (xin_mima_again.getText().toString().isEmpty()){
                    Toast.makeText(ResetActivity.this,"请再次输入新密码",Toast.LENGTH_SHORT).show();
                }else if (!xin_mima.getText().toString().equals(xin_mima_again.getText().toString())){
                    Toast.makeText(ResetActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                }else {
                    xiugai();
                }
                break;
            case R.id.system:
                Intent intent=new Intent(ResetActivity.this,SystemAcitvity.class);
                startActivity(intent);
                break;
        }
    }

    public void select(int position) {
        selectPosition=position;
        name.setText(data.get(position).getFusername());
        popupWindow.dismiss();
    }

    private void xiugai(){
        RequestParams requestParams=new RequestParams(MyApplication.url+"tools/submit_ajax.ashx?action=updateuserpwd");
        requestParams.setConnectTimeout(1000);
        requestParams.addBodyParameter("fuserid", String.valueOf(data.get(selectPosition).getFItemID()));
        requestParams.addBodyParameter("pwd",xin_mima_again.getText().toString());
        x.http().post(requestParams, new Callback.CacheCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson=new Gson();
                ResetHttp http= gson.fromJson(result,ResetHttp.class);
                Toast.makeText(ResetActivity.this,http.getMsg(),Toast.LENGTH_SHORT).show();
                if (http.getStatus()==1){
                    EventBus.getDefault().post(new MyEvent(0,""));
                    finish();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(ResetActivity.this,"网络请求失败，请检查服务器ip",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public boolean onCache(String result) {
                return false;
            }
        });
    }
}
